package org.brisling.supply.queryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.supply.domain.SupplyConsumable;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryConsumableService</p>
 * <p>Description: 供应商产品信息</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年1月6日 下午10:18:56
 */

@RestController
@RequestMapping("/supplyConsumable/query")
public class QueryConsumableService extends AbstractBaseQueryService<SupplyConsumable>{
	
	@Autowired
	private QueryDictionaryService qryDictionaryService;
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	public QueryConsumableService(){
		this.moduleName = ConstSysParam.MODULE_SUPPLY_CONSUMABLE;
		this.subSystem = ConstSysParam.SUBSYSTEM_SUPPLY;
		
		this.clazz = SupplyConsumable.class;
		this.tableName = "V_" +this.moduleName;
		typeMap=this.getTypeMap(clazz);		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}	
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<SupplyConsumable> getListDomain(
			@RequestBody(required=false) String body){	
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
		
	}
	
	/**
	 * 根据传入的数据字典，查询符合sql语句的listDomain
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/getSqlListDomain.action")
	public ListDomain<SupplyConsumable> getSqlListDomain(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,@RequestParam(value="sqlid",required=false) String sqlid){	
		
		String sql = null;
		if(sqlid!=null){
			sql = qryDictionaryService.getSqlByBizcode(sqlid);			
			if(sql!=null){
				return super.getPagedListDomain(page, rows, sql);
			}
		}
		 
		return new ListDomain<SupplyConsumable>();
	}
	
	/**
	 * 根据传入的数据字典和ID，查询符合条件的sql语句的OneDomain
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/getSqlOneDomain.action")
	public ListDomain<SupplyConsumable> getSqlOneDomain(@RequestBody(required=false)String body,@RequestParam(value="sqlid",required=false) String sqlid){	
		
		String sql = null;
		Map<String,Object> conditionMap =getBodyParam(body);	
		if(sqlid!=null){
			sql = qryDictionaryService.getSqlByBizcode(sqlid);			
			return super.getListDomainByConditionAndOrder(sql, conditionMap, typeMap, orderMap, false);
		}
		return new ListDomain<SupplyConsumable>();
	}
	
	
	
	@RequestMapping("/getTreeListDomain.action")
	public ListDomain<SupplyConsumable> getTreeListDomain(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeListDomain(id);
		
	}	
	
	@RequestMapping("/getTreeList.action")
	public List<SupplyConsumable> getTreeList(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeList(id);
		
	}	
	
	@RequestMapping("/getOne.action")
	public SupplyConsumable getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	@RequestMapping("/getMonitorData.action")
	public List<SupplyConsumable> getMonitorData(String sql){
		return super.getAllList(sql);
	}
	
	/**
	 * 批量审核供应商产品信息方法
	 */
	
	@RequestMapping("/auditPage.action")
	public ModelAndView getAuditPage(){
		return new ModelAndView("/supply/supply_consumable/audit");
	}	
	
	
	/**
	 * 获取所有供应商信息
	 * 
	 */
	
	@RequestMapping("/getAllListDomain.action")
	public ListDomain<SupplyConsumable> getAllListDomain(@RequestBody(required=false) String body){		
		
		Map<String,Object> map = this.getBodyParam(body);
		
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,String> paramMap = (Map<String, String>) map.get(ConstSysParam.CONDITION_MAP_KEY);
		
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_supply_baseinfo_all")){
			if(isManageJob(userid)){
				String deptids = this.getJobDeptidAndAccountDeptids(userid);
				paramMap.put("dept_code_in", deptids);
			}else{
				paramMap.put("doct_code", userid);					
			}
			//查找科室数据		
		}else{
			paramMap.put("dept_code_in", "-1");
		}
		
		map.put(ConstSysParam.CONDITION_MAP_KEY, paramMap);		
		
		//查询表
		return super.getListDomainByConditionAndOrder(null, map, typeMap, orderMap, true);
		
	}
	
	/**
	 * 计算供应商的总数
	 * 
	 */
	
     public List<SupplyConsumable> countSupplyConsumable(String year_month,String startDate ,String endDate){

    	 String[] sqlids = {"base_supply_all"};
		//统计全院数据-住院
		
		List<String> sqls = new ArrayList<String>();
		for(int i = 0;i<sqlids.length;i++){
			String sql = qryDictionaryService.getSqlByBizcode(sqlids[i]);
			if(sql!=null){
				sqls.add(sql);
			}
		}
		//所有数据
		List<SupplyConsumable> lists = super.monitorCountBase(year_month,startDate,endDate,sqls);		
		
		return lists;
		
	}
	

}
